package SwordFingerOffer;

public class Q24_ReverseList {

    //反转链表
    //迭代 链表coding无小事
    //cur.next.next = cur这就是错的 围绕它来做自然都是错的
    //已经记录了pre 和 next 就不需要cur.next.next = cur这种操作
    public static ListNode reverseList(ListNode head) {

        if (head == null){
            return null;
        }
        //记录状态
        ListNode pre = null;
        ListNode next = null;
        ListNode cur = head;
        while (cur != null){
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        ListNode node = reverseList(head);
        System.out.println(node);
    }
}
